//
//  XLChannelItem.m
//  XLChannelControlDemo
//
//  Created by MengXianLiang on 2017/3/3.
//  Copyright © 2017年 MengXianLiang. All rights reserved.
//

#import "XLChannelItem.h"

@interface XLChannelItem ()
{
    UILabel *_textLabel;
    
    CAShapeLayer *_borderLayer;
    UIImageView *imageView;
}
@end

@implementation XLChannelItem

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self initUI];
    }
    return self;
}

-(void)initUI
{
    self.userInteractionEnabled = true;
    self.layer.cornerRadius = 10.0f;
    self.backgroundColor = [self backgroundColor];
    
    _textLabel = [UILabel new];
    _textLabel.frame = self.bounds;
    _textLabel.textAlignment = NSTextAlignmentCenter;
    _textLabel.textColor = [self textColor];
    _textLabel.adjustsFontSizeToFitWidth = true;
    _textLabel.userInteractionEnabled = true;
    _textLabel.font =[UIFont systemFontOfSize:13];
    [self addSubview:_textLabel];
    
    
//    imageView = [[UIImageView alloc] init];
//    imageView.image =[UIImage imageNamed:@"caca"];
//    imageView.backgroundColor = [UIColor clearColor];
//    imageView.userInteractionEnabled = YES;
//    [self addSubview:imageView];
   
    
    [self addBorderLayer];
}

-(void)addBorderLayer{
    _borderLayer = [CAShapeLayer layer];
    _borderLayer.bounds = self.bounds;
    _borderLayer.position = CGPointMake(CGRectGetMidX(self.bounds), CGRectGetMidY(self.bounds));
    _borderLayer.path = [UIBezierPath bezierPathWithRoundedRect:_borderLayer.bounds cornerRadius:self.layer.cornerRadius].CGPath;
    _borderLayer.lineWidth = 1;
    _borderLayer.lineDashPattern = @[@5, @3];
    _borderLayer.fillColor = [UIColor clearColor].CGColor;
    _borderLayer.strokeColor = [self backgroundColor].CGColor;
    [self.layer addSublayer:_borderLayer];
    _borderLayer.hidden = true;
}

-(void)layoutSubviews
{
    [super layoutSubviews];
//     if (_isRow) {
//         _textLabel.frame = CGRectMake(0, 0, self.frame.size.width-30, self.frame.size.height);//self.bounds;
//         imageView.frame = CGRectMake(_textLabel.frame.size.width,10, 10, 10);
//     }else{
//          imageView.hidden = YES;
//         _textLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);//self.bounds;
//     }
}

#pragma mark -
#pragma mark 配置方法

-(UIColor*)backgroundColor{
    return [UIColor colorWithRed:241/255.0f green:241/255.0f blue:241/255.0f alpha:1];
}

-(UIColor*)textColor{
    return [UIColor colorWithRed:40/255.0f green:40/255.0f blue:40/255.0f alpha:1];
}

-(UIColor*)lightTextColor{
    return [UIColor colorWithRed:200/255.0f green:200/255.0f blue:200/255.0f alpha:1];
}

#pragma mark -
#pragma mark Setter

-(void)setTitle:(NSString *)title
{
    _title = title;
    _textLabel.text = title;
}

-(void)setIsMoving:(BOOL)isMoving
{
    _isMoving = isMoving;
    if (_isMoving) {
        self.backgroundColor = [UIColor clearColor];
        _borderLayer.hidden = false;
    }else{
        self.backgroundColor = [UIColor greenColor];
        _borderLayer.hidden = true;
    }
}


-(void)setIsRow:(BOOL)isRow{
    _isRow = isRow;
    if (isRow) {
        [imageView removeFromSuperview];
        imageView = [[UIImageView alloc] init];
        imageView.image =[UIImage imageNamed:@"caca"];
        imageView.backgroundColor = [UIColor clearColor];
        imageView.userInteractionEnabled = YES;
        [self addSubview:imageView];
        _textLabel.frame = CGRectMake(0, 0, self.frame.size.width-30, self.frame.size.height);//self.bounds;
        imageView.frame = CGRectMake(_textLabel.frame.size.width,10, 10, 10);
    }else{
        imageView.hidden = YES;
        _textLabel.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);//self.bounds;
    }
}
-(void)setIsFixed:(BOOL)isFixed{
    _isFixed = isFixed;
    if (isFixed) {
        _textLabel.textColor = [self lightTextColor];
    }else{
        _textLabel.textColor = [self textColor];
    }
}


@end
